﻿using SwParaPlatform2.logger;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SwParaPlatform2.dlgFormula
{

    public partial class dlgElementSelect : Form
    {
        public Dictionary<string, string> dSelectedItem { get; private set; }
        private Dictionary<string, string> parentChildMap;
        private readonly ISwParaInterface _commonInterface;
        public dlgElementSelect()
        {
            _commonInterface = new SwParaInterface();
            InitializeComponent();
            Init();
        }

        private void Init()
        {
            TreeBuilder builder = new TreeBuilder();
            var mainMainSubComp = DataStore.Instance.GetMainMainSubComp(DataStore.Instance.TreeViewData);

            if (mainMainSubComp != null)
            {
                foreach (var subComp in mainMainSubComp)
                {
                    DataTreeNode rootNode = builder.CreateNodeTree((Dictionary<string, object>)subComp.Value, subComp.Key);
                    AddNodesToTreeView(treeView1, rootNode);
                }
            }
            parentChildMap = DataStore.Instance.GetParentChildMap();
        }

        public void AddNodesToTreeView(TreeView treeView, DataTreeNode node, bool isRoot = true)
        {
            var treeNode = new System.Windows.Forms.TreeNode(node.Name)
            {
                Tag = node.Data
            };

            if (isRoot)
            {
                // 设置根节点为灰色，并且不可选
                treeNode.ForeColor = Color.Gray;
                treeNode.NodeFont = new Font(treeView.Font, FontStyle.Italic);
            }

            foreach (var child in node.Children)
            {
                AddNodesToTreeNode(treeNode, child, false);
            }

            treeView.Nodes.Add(treeNode);
        }

        private void AddNodesToTreeNode(System.Windows.Forms.TreeNode parentTreeNode, DataTreeNode node, bool isRoot = false)
        {
            var treeNode = new System.Windows.Forms.TreeNode(node.Name)
            {
                Tag = node.Data
            };

            foreach (var child in node.Children)
            {
                AddNodesToTreeNode(treeNode, child, false);
            }

            parentTreeNode.Nodes.Add(treeNode);
        }

        private void dlgElementSelect_Load(object sender, EventArgs e)
        {

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.ForeColor != Color.Gray)
            {
                // 打印选中节点对应的字典内容
                Dictionary<string, object> data = e.Node.Tag as Dictionary<string, object>;
                if (data != null)
                {
                    string dataContent = string.Join(", ", data.Select(kv => $"{kv.Key}: {kv.Value}"));
                    Logger.Instance.Log($"Selected Node Data: {dataContent}");
                }
            }
            else
            {
                // 如果是灰色节点，取消选择
                treeView1.SelectedNode = null;
            }
        }

        private void TreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {

        }
        
        private void SelectAndClose(TreeNode TNode, string PropName)
        {
            Dictionary<string, object> TNodeInfo = (Dictionary<string, object>)TNode.Tag;
            string nodeName = TNodeInfo["TempName"].ToString();
            List<string> path = new List<string> { nodeName };

            string current = nodeName;
            while (parentChildMap.ContainsKey(current))
            {
                current = parentChildMap[current];
                if(current != null && current != "null")
                {
                    path.Insert(0, current);
                }
                
            }

            dSelectedItem = new Dictionary<string, string>
            {
                {"Element", path[path.Count - 1] + PropName},
                {"FullPath",  string.Join("%->%", path) + PropName},
            };

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                SelectAndClose(treeView1.SelectedNode, $"{Defines.UseValue_Split}SUP");
            }
            else
            {
                MessageBox.Show("No node selected.");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                SelectAndClose(treeView1.SelectedNode, $"{Defines.UseValue_Split}DIM");
            }
            else
            {
                MessageBox.Show("No node selected.");
            }
        }
    }

    
}
